/*
 * Project: Code
 * File Created at 2024/1/14
 @Type Maximun_Product_Subarray.java
 @author Goodwin
 * Website : https://leetcode.cn/problems/maximum-product-subarray/description/?envType=study-plan-v2&envId=top-100-liked
*/
package Dynamic_Program;

public class Maximun_Product_Subarray {
    public int maxProduct(int[] nums) {
        int length = nums.length;
        int[] maxF = new int[length];
        int[] minF = new int[length];
        System.arraycopy(nums,0,maxF,0,length);
        System.arraycopy(nums,0,minF,0,length);
        int ans = Integer.MIN_VALUE;
        for (int i = 1; i < length; i++) {
            maxF[i] = Math.max(Math.max(maxF[i-1]*nums[i], minF[i-1]*nums[i]), nums[i]);
            minF[i] = Math.min(Math.min(minF[i-1]*nums[i], maxF[i-1]*nums[i]), nums[i]);
            ans = Math.max(ans, maxF[i]);
        }
        return ans;
    }
}
